﻿using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Collections;
using Telerik.Windows.Controls;
using System.Threading;
using TestMatrix.Model;
using TestMatrix.BLController;

namespace TestMatrix.Phone
{
    public partial class TestCaseReport : PhoneApplicationPage
    {
        public TestCaseReport()
        {
            InitializeComponent();
            this.Loaded += OnLoad;
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            if (this.ListBox != null)
            {
                this.ListBox.IsAsyncBalanceEnabled = this.EnableAsyncBalance;
            }

            this.isDisplayed = true;
            this.OnDisplayed();
        }

        protected virtual IEnumerable CreateItemsSource()
        {
            string sql = "select ";
            sql += "Application," + Environment.NewLine;
            sql += "sum(TotalTestCases) as TotalTestCases," + Environment.NewLine;
            sql += "sum(Automatable) as Automatable," + Environment.NewLine;
            sql += "sum(CannotAutomated) CannotAutomated," + Environment.NewLine;
            sql += "sum(AutomatableNotReady) AutomatableNotReady," + Environment.NewLine;
            sql += "sum(AlreadyAutomated) AlreadyAutomated," + Environment.NewLine;
            sql += "sum(Gap) as Gap," + Environment.NewLine;
            sql += "sum(AlreadyAutomated*100)/cast(sum(Automatable) as double) as RateAlreadyAutomated," + Environment.NewLine;
            sql += "min(LinesOfAutomationCode) LinesOfAutomationCode," + Environment.NewLine;
            sql += "sum(Automatable*100)/cast(sum(TotalTestCases)  as double) as RateAutomatable" + Environment.NewLine;
            sql += "from tblTestAutomation" + Environment.NewLine;
            sql += "group by Application;";

            List<tblTestAutomation> items = ChartController.QueryTblTestAutomation(sql);

            return items;
        }

        private List<tblTestAutomation> GetSingleApplication()
        {
            string sql = "select Application from tblTestAutomation group by Application;";
            List<tblTestAutomation> items = ChartController.QueryTblTestAutomation(sql);
            return items;
        }

        #region basic method

        private IEnumerable itemsSource;
        private bool isDisplayed;

        protected virtual RadDataBoundListBox ListBox
        {
            get
            {
                return this.DataBoundListBox;
            }
        }

        public bool IsDisplayed
        {
            get
            {
                return this.isDisplayed;
            }
        }

        protected virtual void OnDisplayed()
        {
            if (this.ListBox == null)
            {
                return;
            }

            if (this.itemsSource == null)
            {
                ThreadPool.QueueUserWorkItem(this.LoadItemsSource);
            }
            else
            {
                this.DisplayData(this.itemsSource);
            }
        }

        private void LoadItemsSource(object state)
        {
            this.itemsSource = this.CreateItemsSource();
            if (this.isDisplayed)
            {
                this.DisplayData(this.itemsSource);
            }

        }

        protected virtual bool EnableAsyncBalance
        {
            get
            {
                return true;
            }
        }

        protected virtual void DisplayData(IEnumerable items)
        {
            if (this.ListBox == null || !this.isDisplayed)
            {
                return;
            }

            this.Dispatcher.BeginInvoke(() =>
            {
                this.ListBox.BeginAsyncBalance();
                this.ListBox.ItemsSource = items;
                this.OnItemsBound();

            });
        }

        protected virtual void OnItemsBound()
        {
        }

        #endregion

        private void DataBoundListBox_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            string text = "";
            try
            {
                text = ((TextBlock)e.OriginalSource).Text;
            }
            catch (Exception ex)
            {

            }

            List<tblTestAutomation> lstApps = GetSingleApplication();

            if (lstApps == null || lstApps.Count == 0)
                return;
            foreach (var v in lstApps)
            {
                if (text.Equals(v.Application))
                {
                    //System.Windows.MessageBox.Show("aaa" + e.OriginalSource.ToString() + Environment.NewLine + text);
                    NavigationService.Navigate(new Uri("/TestCaseReportPivot.xaml?PrjName=" + v.Application, UriKind.Relative));
                }
            }
        }
    }
}